<?php
require_once 'inc/MCAPI.class.php';
require_once '../../../wp-blog-header.php';

$apikey = 'bdfdc9236a45ff03c3533c5a2d3ae9c7-us1'; 
$api = new MCAPI2('bdfdc9236a45ff03c3533c5a2d3ae9c7-us1');
$date = date('Y-m-d');
$date = '%'.$date.'%';
$list_city = 'Mumbai';

$retval = $api->listMembers('6ffad0c8eb', 'subscribed', null, 0, 5000);

foreach($retval['data'] as $member){
    $emails[] = $member['email'];
}
foreach($emails as $email){
   $membersInfo[] = $api->listMemberInfo( '6ffad0c8eb', array($email) ); 
}   

foreach($membersInfo as $key=>$details){ 
   $info = $details['data'][0]['merges'];
   $members_ses[$key]['email'] = $info['EMAIL'];
   $members_ses[$key]['city'] = $info['MMERGE2'];
   $members_ses[$key]['interests'] = $info['GROUPINGS'][0]['groups'];
}

foreach($members_ses as $sorted_k=>$sorted)
{
  if($sorted['city'] == $list_city)
  {
    $members_city[$sorted_k]['email'] = $sorted['email'];
    $members_city[$sorted_k]['interests'] = $sorted['interests'];
  }  
}
echo "<pre>";
//print_r($members_city);
//exit();
$Record = new Pod('deal');
foreach($members_ses as $value){
  unset($interests);
  $interests = explode(',',$value['interests']);
  print_r($interests);
 
  unset($interests_final);
  foreach($interests as $k=>$v){
    $interests_final[$k] = 'c-'.sanitize_title($v).'-offers';
  }
  
  $count = count($interests_final);
  unset($interests_ses);
  for($i=0;$i<$count;$i++){
    $interests_ses .= "'".$interests_final[$i]."'";
    
    if($i != ($count - 1))
    {
     $interests_ses .= ","; 
    } 
  }

  $where = "city.name = '".strtolower($value['city'])."' && camp_cat.url IN(".$interests_ses.") && t.camp_end > DATE_ADD(NOW(), INTERVAL '05:30' HOUR_MINUTE) && t.camp_start LIKE '".$date."' && t.daily = 1";
  $Record->findRecords('id DESC', '',$where);
  $dealsFound = $Record->getTotalRows();
  echo $dealsFound;
  
  unset($deal);
  $j=0;
  while($Record->fetchRecord()){
    $deal[$j]['name'] = stripslashes($Record->get_field('name'));
    $deal[$j]['link'] = get_bloginfo('url').'/deals/'.$Record->get_field('camp_link');
    $deal_image = $Record->get_field('image.guid');
    if(empty($deal_image))
    {
      $deal_image = $Record->get_field('ext_image_url');
    }
    $deal[$j]['image'] = $deal_image;
    $j++;
  }
  
  unset($html);
  //print_r($deal);
  
  if(count($deal) > 0)
  {
  
  $html = '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
  <table width="620" border="0" align="center" cellpadding="10" cellspacing="0" > 
    <tr>
      <td valign="top">
        <span >{Intro}</span>
        <br />
        <a name="top"></a>
            
        <div align="center">    
            
            <table border="0" cellpadding="0" cellspacing="0" width="620" style="border: 1px solid #000000; padding: 5px;" bgcolor="#FFFFFF">
              <tr>
                <td valign="top">
            
              
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
              <tr>
                <td valign="bottom">
              	<div align="center">
                		<table border="0" cellpadding="0" cellspacing="0">
                  		<tr>
                    			<td width="100%">
							              <div style="margin-bottom: 10px; margin-top: 10px; text-align: center;"><img style="width: 70%;" src="http://buzzr.in/wordpress/wp-content/uploads/2011/01/buzzrin-logo.png" /></div>
              					    <div mc:edit="newsletter-name" style="margin-bottom: 10px; letter-spacing: 0pt; font-family: Impact; font-size: 20pt; color: #C60000" align="center"><font face="Impact" color="#C60000">{Newsletter title}</font></div>
                    			</td>
                  		</tr>
                		</table>
              	</div>
                </td>
              </tr>
            </table>
            <!--  
            <table border="0" cellpadding="0" cellspacing="0" width="100%" height="30">
            	<tr>
              	<td bgcolor="#000000" style="padding-left:3px;">
              		<p mc:edit="date" align="left" style="padding-left: 6pt; font-family: Arial; font-size: 9pt; color:#FFFFFF"><b>Daily Deals for Buzzr.in for *|DATE:d/m/y|*</b>
              </tr>
            </table>
            -->
            <div style="border-bottom: 1px solid black; margin-bottom: 20px;"></div>
<div style="margin-left: 10px">';
foreach($deal as $deal_value){
 $html .= '<div style="margin-right: 10px;"><a href="'.$deal_value['link'].'" title="'.$deal_value['name'].'" target="_blank">'.$deal_value['name'].'</a></div><br /><div style="width: 600px;"><img height="120" width="180" src="'.$deal_value['image'].'" /></div> <br />';
}

$html .= '</div></body></html>';  

print_r($html);
exit();
/*  
  //$to_emails = array($value['email']);
  $to_emails = array('info@enthrallmedia.com');
  //$to_names = array($value['email']);
  $to_names = array('info@enthrallmedia.com');
 
  $message = array(
    'html'=>'<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
  <table width="620" border="0" align="center" cellpadding="10" cellspacing="0" > 
    <tr>
      <td valign="top">
        <span >{Intro}</span>
        <br />
        <a name="top"></a>
            
        <div align="center">    
            
            <table border="0" cellpadding="0" cellspacing="0" width="620" style="border: 1px solid #000000; padding: 5px;" bgcolor="#FFFFFF">
              <tr>
                <td valign="top">
            
              
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
              <tr>
                <td valign="bottom">
              	<div align="center">
                		<table border="0" cellpadding="0" cellspacing="0">
                  		<tr>
                    			<td width="100%">
							              <div style="margin-bottom: 10px; margin-top: 10px; text-align: center;"><img style="width: 70%;" src="http://buzzr.in/wordpress/wp-content/uploads/2011/01/buzzrin-logo.png" /></div>
              					    <div mc:edit="newsletter-name" style="margin-bottom: 10px; letter-spacing: 0pt; font-family: Impact; font-size: 20pt; color: #C60000" align="center"><font face="Impact" color="#C60000">{Newsletter title}</font></div>
                    			</td>
                  		</tr>
                		</table>
              	</div>
                </td>
              </tr>
            </table>
            <!--  
            <table border="0" cellpadding="0" cellspacing="0" width="100%" height="30">
            	<tr>
              	<td bgcolor="#000000" style="padding-left:3px;">
              		<p mc:edit="date" align="left" style="padding-left: 6pt; font-family: Arial; font-size: 9pt; color:#FFFFFF"><b>Daily Deals for Buzzr.in for *|DATE:d/m/y|*</b>
              </tr>
            </table>
            -->
            <div style="border-bottom: 1px solid black; margin-bottom: 20px;"></div>
<div style="margin-left: 10px">

</div>
</body>
</html>',

$message = array(
    'html'=> $html,
    'text'=>'Customized Deals for info@enthrallmedia.com',
    'subject'=>'Buzzr.in: Daily Deals for info@enthrallmedia.com',
    'from_name'=>'Buzzr.in',
    'from_email'=>'info@enthrallmedia.com',
    'to_email'=>$to_emails,
    'to_name'=>$to_names
);

$tags = array('Deals','Offers','Discounts','Vouchers','Coupons');
 
$params = array(
    'apikey'=>$apikey,
    'message'=>$message,
    'track_opens'=>true,
    'track_clicks'=>true,
    'tags'=>$tags
);
 
$url = "http://us1.sts.mailchimp.com/1.0/SendEmail";
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
$result = curl_exec($ch);
echo $result;
curl_close ($ch);
 
$data = json_decode($result);
echo "Status = ".$data->status."\n";
}
*/
}
}
?>
  